﻿using Exiled.API.Features;
using Exiled.API.Features.Pools;
using RgCore.GameModes;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using VotingSystem;

namespace GameModeVote
{
    [Vote("更改模式", 0.5)]
    public class GameModeVote : Votable
    {
        public override string BroadcastTitle()
        {
            return $"更改下回合模式为 <color=yellow>[{Selection.Value.Text}]</color>";
        }

        public override IEnumerable<Selection> GetSelections()
        {
            var list = ListPool<Selection>.Pool.Get();
            foreach (var gamemode in GameMode.AllGameModes)
            {
                list.Add(new Selection()
                {
                    Text = gamemode.GetCustomAttribute<GameModeAttribute>().Name,
                    Parameters = new object[] { gamemode }
                });
            }
            var array = list.ToArray();
            ListPool<Selection>.Pool.Return(list);
            return array;
        }

        public override bool Launchable(Player player, out string response, Selection? selection)
        {
            if (selection.HasValue)
            {
                var type = selection.Value.Parameters.First() as Type;
                if (GameMode.NextGameMode == type)
                {
                    response = "下一回合模式已是本模式";
                    return false;
                }
                if (!player.RemoteAdminAccess && type.GetCustomAttribute<GameModeAttribute>().AdminOnly)
                {
                    response = "此模式只能由管理员启动";
                    return false;
                }
            }
            response = "";
            return true;
        }

        public override string OnPassed()
        {
            GameMode.SetNextGameMode(Selection.Value.Parameters.First() as Type);
            return "";
        }
    }
}